#!/usr/bin/env python
# $Id: dnsupdate.py df54dec975f4 2012/09/09 20:22:40 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: df54dec975f4 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-09-09 20:22:40Z $"

import optparse
import os
import urllib

from urllib import FancyURLOpener

from venya.lib import cmdparser, db4, stdlib

class DNSOpener(FancyURLOpener):
   version = "dnsupdater No-IP Updater/1.0 pgurumur@gmail.com"

class dnsupdater(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-d", "--database", type = "str",
            help = "Berkeley database to use for no-ip",
            default = "/etc/noip.db", dest = "database"),
         optparse.make_option("-e", "--email", type = "str",
            help = "Email address associated with no-ip", dest = "email"),
         optparse.make_option("-s", "--site", type = "str",
            help = "Site name associated with no-ip", dest = "site"),
         optparse.make_option("-u", "--update", action = "store_true",
            help = "Update DNS entry in no-ip database", dest = "update"),
         optparse.make_option("-r", "--revise", action = "store_true",
            help = "Local update for the databse", dest = "revise"),
   ) + cmdparser.GetOpt.option_list

   def __init__(self, **kwargs):
      super(stdlib.my_class(self), self).__init__(**kwargs)
      self._opener = DNSOpener()

   def handle(self):
      (options, args) = self.parse()

      emailaddr = None
      if options.email:
         emailaddr =  options.email.strip()

      if emailaddr:
         emailpass = getpass.getpass("%s password on no-ip.org: " %emailaddr)



